/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewReportForm.java
 *
 * Created on Apr 12, 2012, 1:05:43 AM
 */
package GUI;

import entities.PatientDetails;
import entities.ReportDetails;
import entities.StockDetail;
import entities.Test;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.patient.PatientComboBoxModel;
import model.stock.StockComboBoxModel;
import model.test.TestComboBoxModel;
import utilities.ReportManage;

/**
 *
 * @author VietLH
 */
public class AddNewReportForm extends javax.swing.JDialog {

    /** Creates new form AddNewReportForm */
    public AddNewReportForm(java.awt.Frame parent, boolean modal,
            ArrayList<Test> testList, ArrayList<PatientDetails> patientList, ReportManage rm, MainFunction parentFrame) {
        super(parent, modal);
        initComponents();
        this.testList = new ArrayList<Test>();
        this.testList = testList;
        TestComboBoxModel tcm = new TestComboBoxModel(testList);
        cbTestName.setModel(tcm);
        this.patientList = new ArrayList<PatientDetails>();
        this.patientList = patientList;
        PatientComboBoxModel pcm = new PatientComboBoxModel(patientList);
        cbPatientName.setModel(pcm);
        this.rm = new ReportManage();
        this.rm = rm;
        this.parent = parentFrame;
        this.optionMessage=new OptionMessage(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblPatientName = new javax.swing.JLabel();
        lblTestName = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        cbPatientName = new javax.swing.JComboBox();
        cbTestName = new javax.swing.JComboBox();
        lblDOB = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblDOBDtl = new javax.swing.JLabel();
        lblAddressDtl = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add New Report", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), java.awt.Color.blue)); // NOI18N

        lblPatientName.setText("Patient Name:");

        lblTestName.setText("Test Name:");

        btnAdd.setText("Add");
        btnAdd.setEnabled(false);
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        cbPatientName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbPatientName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbPatientNameActionPerformed(evt);
            }
        });

        cbTestName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbTestName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTestNameActionPerformed(evt);
            }
        });

        lblDOB.setText("Date of Birth:");

        lblAddress.setText("Address:");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel)
                        .addGap(23, 23, 23))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPatientName)
                            .addComponent(lblTestName)
                            .addComponent(lblDOB)
                            .addComponent(lblAddress))
                        .addGap(62, 62, 62)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDOBDtl, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                            .addComponent(cbPatientName, 0, 238, Short.MAX_VALUE)
                            .addComponent(lblAddressDtl, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                            .addComponent(cbTestName, javax.swing.GroupLayout.Alignment.TRAILING, 0, 238, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPatientName)
                    .addComponent(cbPatientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDOB)
                    .addComponent(lblDOBDtl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAddress)
                    .addComponent(lblAddressDtl))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbTestName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTestName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnCancel))
                .addGap(34, 34, 34))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbPatientNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbPatientNameActionPerformed
        // TODO add your handling code here:
        setPatientDetail();
    }//GEN-LAST:event_cbPatientNameActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        addNewReport();
    }//GEN-LAST:event_btnAddActionPerformed

    private void cbTestNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTestNameActionPerformed
        // TODO add your handling code here:
        checkForEnableAddBtn();
    }//GEN-LAST:event_cbTestNameActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        closeForm();
    }//GEN-LAST:event_btnCancelActionPerformed
    private void checkForEnableAddBtn() {
        if (((PatientDetails) cbPatientName.getSelectedItem()).getpID() > 0 && ((Test) cbTestName.getSelectedItem()).getTestID() > 0) {
            this.btnAdd.setEnabled(true);
        } else {
            this.btnAdd.setEnabled(false);
        }
    }

    private void closeForm() {
        optionMessage.confirmExit();
    }

    private void setPatientDetail() {
        PatientDetails pdt = new PatientDetails();
        pdt = (PatientDetails) cbPatientName.getSelectedItem();
        this.lblAddressDtl.setText(pdt.getpAddress());
        this.lblDOBDtl.setText(pdt.getpDOB().toString());
        checkForEnableAddBtn();
    }

    private void addNewReport() {
        if (((PatientDetails) cbPatientName.getSelectedItem()).getpID() > 0 && ((Test) cbTestName.getSelectedItem()).getTestID() > 0) {
            ReportDetails rd = new ReportDetails();
            rd.setPatientDetails((PatientDetails) cbPatientName.getSelectedItem());
            rd.setTest((Test) cbTestName.getSelectedItem());
            rd.setTestDate(new Date());
            rd.setStatus(-1);
            if (rm.createNewReport(rd)) {
                this.dispose();
            }
        }
    }
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(AddNewReportForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(AddNewReportForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(AddNewReportForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(AddNewReportForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                AddNewReportForm dialog = new AddNewReportForm(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JComboBox cbPatientName;
    private javax.swing.JComboBox cbTestName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblAddressDtl;
    private javax.swing.JLabel lblDOB;
    private javax.swing.JLabel lblDOBDtl;
    private javax.swing.JLabel lblPatientName;
    private javax.swing.JLabel lblTestName;
    // End of variables declaration//GEN-END:variables
    private ArrayList<Test> testList;
    private ArrayList<PatientDetails> patientList;
    private ReportManage rm;
    private MainFunction parent;
    private OptionMessage optionMessage;
}
